<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
namespace Publish\Controller;

use Common\Controller\AdminbaseController;

class AdminInforController extends AdminbaseController {

	protected $pub_image_model;

	function _initialize() {
		parent::_initialize();
		$this->pub_image_model = D("PubImage");
	}

	function index() {
//		$inforImages = $this->pub_image_model
//			->alias("a")
//			->join("__PUB_PERSON__ as b ON a.person_id = b.id ")
//			->join("__PUB_INFORMATION__ as c ON a.infor_id = c.id")
//			->field("a.*,b.name,c.title")
//			->where(array('img_mark' => 2))
//			->select();
//
//		//将得到的数组以人物分组显示
//		foreach ($inforImages as $k => $v) {
//			$res[$v['person_id']][] = $v;
//		}
//
//		$this->assign('res', $res);
//		$this->display();
		$this->_lists();
	}

	function add() {
		$this->display();
	}

	function add_post() {
		/*添加图片信息到图片库*/
		$person_id = I('person_id');
		$infor_id = I('inforId');
		$infor_id || $this->error("其他资料不能为空");
		$_POST['photos_url'] || $this->error("图片不能为空");
		for ($i = 0; $i < count($_POST['photos_url']); $i++) {
			$addImage = \Publish\Lib\ImageLib::AddImages($person_id, $_POST['photos_name'][$i], $_POST['photos_alt'][$i], $_POST['photos_url'][$i], '2',$infor_id);
			empty($addImage) && $this->error("图片添加失败");
		}
		$this->success("添加成功");
	}


	function edit(){
		$id = I("get.id",0,'intval');
		$img_infor = \Publish\Lib\ImageLib::isImage($id);
		$this->assign($img_infor);
		$this->display();
	}

	function edit_post(){
		$id = I("id",0,'intval');
		/*查询原信息--图片如果改动删除原资源*/
		$original = \Publish\Lib\ImageLib::isImage($id);
		$person_id = I('person_id');
		$infor_id = I('inforId');
		$img_url = I('img_url');
		$img_name = I('img_name');
		$img_text = I('img_text');
		$img_mark = '2';

		if(!empty($img_url)){
			//判断是否修改图片文件 $is_status 删除状态
			if($img_url != $original['img_url']) if(!empty($original['img_url'])) $is_status = \Publish\Lib\ImiLib::UnlickFile($original['img_url']);
			$img_url = sp_asset_relative_url($img_url);
		}
		$is_statu = \Publish\Lib\ImageLib::SaveImages($id,$person_id,$img_name,$img_text,$img_url,$img_mark,$infor_id);
		$is_statu ?$this->success("更改成功") :$this->error("更改失败");
	}

	function delete(){
		if(isset($_GET['id'])){
			$id = I("get.id",0,'intval');
			/*删除图片资源*/
			$imgInfor = \Publish\Lib\ImageLib::isImage($id);
			if($imgInfor){
				$is_status = \Publish\Lib\ImiLib::UnlickFile($imgInfor['img_url']);
				$is_status || $this->error("删除失败！");
			}
			if ($this->pub_image_model->where(array('id'=>$id))->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}

		if(isset($_POST['ids'])){
			$ids = I('post.ids/a');
			for($i=0;$i<count($ids);$i++){
				//删除时，删除对应音频文件
				$imgInfor = \Publish\Lib\ImageLib::isImage($ids[$i]);
				//$is_status 删除状态
				if(!empty($imgInfor['img_url'])) $is_status = \Publish\Lib\ImiLib::UnlickFile($imgInfor['img_url']);
				$is_status || $this->error("删除失败！");
			}
			if ($this->pub_image_model->where(array('id'=>array('in',$ids)))->delete()) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}

	function _lists($where=array()){
		/*搜索条件*/
		$start_time=I('request.start_time');
		if(!empty($start_time)){
			$where['a.createtime']=array(
				array('EGT',strtotime($start_time))
			);
		}
		$end_time=I('request.end_time');
		if(!empty($end_time)){
			if(empty($where['a.img_time'])){
				$where['a.img_time']=array();
			}
			array_push($where['a.img_time'], array('ELT',strtotime($end_time)));
		}
		$keyword=I('request.keyword');
		if(!empty($keyword)){
			$where['c.title|b.name']=array('like',"%$keyword%");
		}

		$count =  $this->pub_image_model
			->alias("a")
			->join("__PUB_PERSON__ as b ON a.person_id = b.id ")
			->join("__PUB_INFORMATION__ as c ON a.infor_id = c.id")
			->where(array('img_mark' => 2))
			->where($where)
			->count();
		$page = $this->page($count, 5);
		$inforImages = $this->pub_image_model
			->alias("a")
			->join("__PUB_PERSON__ as b ON a.person_id = b.id ")
			->join("__PUB_INFORMATION__ as c ON a.infor_id = c.id")
			->field("a.*,b.name,c.title")
			->where(array('img_mark' => 2))
			->where($where)
			->limit($page->firstRow , $page->listRows)
			->select();
		//将得到的数组以人物分组显示
		foreach ($inforImages as $k => $v) {
			$res[$v['person_id']][] = $v;
		}

		$this->assign("formget",array_merge($_GET,$_POST));
		$this->assign("page", $page->show('Admin'));
		$this->assign('res', $res);
		$this->display();
	}
}
